from imaplib import *
import sys
import os
import easygui
import thread

def upload():
	archive = sys.argv[1]
	username = sys.argv[2]
	password = sys.argv[3]
	template = sys.argv[4]

	server = IMAP4_SSL("imap.gmail.com", 993)
	server.login(username, password)

	server.create("Python Imap")

	template = open(template, 'r').read()

	if ( os.name != 'nt' ):
		import base64
		attachment = open(archive, 'r').read()
		message = base64.encodestring(attachment)
	else:
		message = os.popen2("base64.exe" + " -e " + "\"" + archive + "\"")[1].read()

	server.append("Python Imap","","",template + message + "\n--FIN--" )

	print server.list()[1]
	print 'Success'
	thread.interrupt_main()

upload_process = thread.start_new_thread(upload, ())
easygui.msgbox(msg="Uploading...", title="SyncFox", ok_button="Cancel")
